<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>图书管理系统</title>

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <!-- ico 图标-->
  <link rel="shortcut icon" href="../images/favicon.ico" />
  <!-- css 文件-->
  <link rel="stylesheet" href="../css/reset.css">
  <link rel="stylesheet" href="../css/selectList.css" />
  <!-- 引入element-ui样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 引入element-ui组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <!-- axios在线地址 -->
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
<div id="app" background>
  <div class="content-header">

    <h1>图书管理</h1>

  </div>
  <div class="app-container">

    <div class="box">

      <div class="filter-container">
        <el-input placeholder="姓名" v-model="pagination.name" style="width: 200px;" class="filter-item"></el-input>
        <el-input placeholder="密码" v-model="pagination.pwd" style="width: 200px;" class="filter-item"></el-input>
        <el-input placeholder="状态" v-model="pagination.state" style="width: 200px;" class="filter-item"></el-input>
        <el-button @click="getAll()" class="dalfBut">查询</el-button>
        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
      </div>

      <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

        <el-table-column prop="id" align="center" label="id"></el-table-column>

        <el-table-column prop="name" label="姓名" align="center"></el-table-column>

        <el-table-column prop="pwd" label="pwd" align="center"></el-table-column>

        <el-table-column prop="state" label="state" align="center"></el-table-column>

        <el-table-column label="操作" align="center">

          <template slot-scope="scope">

            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>

      <!--分页组件-->
      <div class="pagination-container">

        <el-pagination
                class="pagiantion"

                @current-change="handleCurrentChange"

                :current-page="pagination.currentPage"

                :page-size="pagination.pageSize"

                layout="total, prev, pager, next, jumper"

                :total="pagination.total">

        </el-pagination>

      </div>

      <!-- 新增标签弹层 -->

      <div class="add-form">

        <el-dialog title="新增学生" :visible.sync="dialogFormVisible">

          <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

            <el-row>

              <el-col :span="12">

                <el-form-item label="姓名" prop="name">

                  <el-input v-model="formData.name"/>

                </el-form-item>

              </el-col>

              <el-col :span="12">

                <el-form-item label="密码" prop="pwd">

                  <el-input v-model="formData.pwd"/>

                </el-form-item>

              </el-col>

            </el-row>


            <el-row>

              <el-col :span="24">

                <el-form-item label="state">

                  <el-input v-model="formData.state" type="input"></el-input>

                </el-form-item>

              </el-col>

            </el-row>

          </el-form>

          <div slot="footer" class="dialog-footer">

            <el-button @click="cancel()">取消</el-button>

            <el-button type="primary" @click="handleAdd()">确定</el-button>

          </div>

        </el-dialog>

      </div>

      <!-- 编辑标签弹层 -->

      <div class="add-form">

        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

          <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

            <el-row>

              <el-col :span="12">

                <el-form-item label="姓名" prop="name">

                  <el-input v-model="formData.name"/>

                </el-form-item>

              </el-col>

              <el-col :span="12">

                <el-form-item label="密码" prop="pwd">

                  <el-input v-model="formData.pwd"/>

                </el-form-item>

              </el-col>

            </el-row>

            <el-row>

              <el-col :span="24">

                <el-form-item label="状态">

                  <el-input v-model="formData.state" type="input"></el-input>

                </el-form-item>

              </el-col>

            </el-row>

          </el-form>

          <div slot="footer" class="dialog-footer">

            <el-button @click="cancel()">取消</el-button>

            <el-button type="primary" @click="handleEdit()">确定</el-button>

          </div>

        </el-dialog>

      </div>

    </div>

  </div>

</div>

<script>
  new Vue({
    el: '#app',
    data:{
      id: '',
      pwd: "",
      name: "",
      state: "",
      tableData: [],
      dataList: [],//当前页要展示的列表数据
      dialogFormVisible: false,//添加表单是否可见
      dialogFormVisible4Edit:false,//编辑表单是否可见
      formData: {},//表单数据
      rules: {//校验规则
        type: [{ required: true, message: '姓名为必填项', trigger: 'blur' }],
        name: [{ required: true, message: '姓名为必填项', trigger: 'blur' }]
      },
      pagination: {//分页相关模型数据
        currentPage: 1,//当前页码
        pageSize:5,//每页显示的记录数
        total:0,//总记录数
        name: "",
        state: "",
        pwd: "",
        txt: ''
      }
    },
    created() {
      if (localStorage.getItem("user") == null) {
        location.href="/backend/page/Login.html"
      }
      //调用查询全部数据的操作
      this.getAll();
    },
    methods: {

      //条件查询
      //分页查询
      getAll() {
        para = "?query"
        console.log("this.pagination ======== " + this.pagination.pwd)
        if (this.pagination.name!='') {
          para += '&name=' + this.pagination.name
        }
        console.log(this.pagination.name)
        if (this.pagination.pwd!='') {
          para += '&pwd=' + this.pagination.pwd
        }
        if (this.pagination.state!='') {
          para += '&state=' + this.pagination.state
        }
        if (this.pagination.name == '' && this.pagination.state == '' && this.pagination.pwd == '') {
          para = ''
        }
        console.log(para)
        axios.get("/user" + "/" + this.pagination.currentPage + "/" + this.pagination.pageSize + para)
                .then(res => {
                  this.pagination.pageSize = res.data.data.size
                  this.pagination.currentPage = res.data.data.current
                  this.pagination.total = res.data.data.total
                  this.dataList = res.data.data.records
                })
      },

      //切换页码
      handleCurrentChange(currentPage) {
        // 改变页码值
        this.pagination.currentPage = currentPage
        // 执行查询
        console.log(this.pagination.currentPage)
        this.getAll()
      },

      //弹出添加窗口
      handleCreate() {
        this.dialogFormVisible = true
        this.formData = {}
      },

      //重置表单
      resetForm() {
        this.formData = {}
      },

      //添加
      handleAdd () {
        axios.post("/user",this.formData).then( res => {
          console.log(res.data)
          if (res.data.flag == false) {
            this.$message.error("添加失败")
            this.formData = {}
          }else {
            this.dialogFormVisible = false
            this.$message.success("添加成功")
            this.getAll();
          }
        })
      },

      //取消
      cancel(){
        this.dialogFormVisible = false
        this.dialogFormVisible4Edit = false
        this.$message.info("当前操作取消")
      },

      // 删除
      handleDelete(row) {
        this.$confirm("此条数据永久删除,是否继续","提示",{}).then( () => {
          axios.delete("/user/" + row.id).then( res => {
            if (res.data.flag == true) {
              this.$message.success("删除成功")
            }else {
              this.$message.error("数据同步失败，自动刷新")
            }
          }).finally(() => {
            this.getAll();
          })
        }).catch(() => {
          this.$message.info("取消操作")
        })
      },

      //弹出编辑窗口
      handleUpdate(row) {
        axios.get("/user/" + row.id).then( res => {
          if (res.data.flag == true && res.data.data != null) {
            this.dialogFormVisible4Edit = true
            this.formData = res.data.data
          }else {
            this.$message.error('数据同步失败，自动刷新')
          }
        }).finally(() => {
          this.getAll()
        })
      },

      //修改
      handleEdit() {
        axios.put("/user", this.formData).then( res => {
          if (res.data.flag == true) {
            this.$message.success("修改成功")
            this.dialogFormVisible4Edit = false
          }else {
            this.$message.error('修改失败')

          }
        }).finally(() => {
          this.getAll()
        })
      },

    }
  })
</script>
</body>

</html>